import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * jedis 进行 redis 中的一些通用命令
 */
public class RedisDemoGeneric {

    public static void test1(Jedis jedis) {
        System.out.println("get 和 set");
        jedis.flushDB();

        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        jedis.set("k3", "v3");

        String result = jedis.get("k1");
        System.out.println("result: " + result);

        result = jedis.get("k2");
        System.out.println("result: " + result);

        result = jedis.get("k4");
        System.out.println("result: " + result);
    }

    public static void test2(Jedis jedis) {
        System.out.println("exists 和 del");
        jedis.flushDB();

        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        jedis.set("k3", "v3");

        boolean result = jedis.exists("k1");
        System.out.println("result: " + result);
        long count = jedis.del("k1");
        System.out.println("count: " + count);
        result = jedis.exists("k1");
        System.out.println("result: " + result);
    }
    public static void test3(Jedis jedis) {
        System.out.println("keys");
        jedis.flushDB();

        jedis.set("hello", "v1");
        jedis.set("hallo", "v1");
        jedis.set("hbllo", "v1");
        jedis.set("heeeeeeeeeeeeeello", "v1");

        Set<String> result = jedis.keys("*");
        System.out.println("resutl: " + result);

        result = jedis.keys("h?llo");
        System.out.println("resutl: " + result);
    }
    public static void test4(Jedis jedis) {
        System.out.println("expire 和 ttl");
        jedis.flushDB();

        jedis.set("k1", "v1");
        jedis.set("k2", "v2");

        jedis.expire("k1", 5);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long time = jedis.ttl("k1");
        System.out.println("time: " + time);

    }

    public static void test5(Jedis jedis) {
        System.out.println("type");
        jedis.flushDB();

        jedis.set("k1","v1");
        String type = jedis.type("k1");
        System.out.println("k1: " + type);

        jedis.lpush("k2","111","222");
        type = jedis.type("k2");
        System.out.println("k2: " + type);

        jedis.sadd("k3", "111","222");
        type = jedis.type("k3");
        System.out.println("k3: " + type);

        jedis.zadd("k4", 90.0, "zhangsan");
        type = jedis.type("k4");
        System.out.println("k4: " + type);

        jedis.hset("k5", "f1", "v1");
        type = jedis.type("k5");
        System.out.println("k5: " + type);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            // test1(jedis);
            // test2(jedis);
            // test3(jedis);
            // test4(jedis);
            test5(jedis);
        }
    }
}
